﻿Imports System.Windows.Forms

Public Class frm_main

    Dim MyThread As Threading.Thread

    Public Sub StartThread()
        MyThread = New Threading.Thread(AddressOf ChekStatus)
        MyThread.Start()
    End Sub

    Public Sub StopThread()
        Try
            MyThread.Abort()
        Catch ex As Exception

        End Try
    End Sub

    Delegate Sub delegChangeStatus(ByVal status As String)

    Sub safeChangeStatus(ByVal status As String)
        If status = "ONLINE" Then
            Me.StatusOnline.Text = status
            Me.StatusOnline.BackColor = Color.GreenYellow
        Else
            Me.StatusOnline.Text = status
            Me.StatusOnline.BackColor = Color.MistyRose
        End If
    End Sub

    Sub ChekStatus()

        If SignalUpload = False Then ' jika tidak sedang upload
            ' cek koneksi
            If SOCKET.IsConnected = True Then
                If SOCKET.SendMessage("PING") = "Error" Then ' send data untuk cek koneksi ke server
                    Me.Invoke(ChangeStatus, "OFFLINE")
                    SOCKET.Disconnect()
                Else
                    Me.Invoke(ChangeStatus, "ONLINE")
                End If
            Else
                If SOCKET.Connect() Then
                    SOCKET.SendMessage("EHLO server")
                    SOCKET.SendMessage("USER " & APP.Info.KD_Sekolah)
                    SOCKET.SendMessage("PASS " & APP.Info.KD_Sekolah)
                    SOCKET.SendMessage("LOGN")
                    Me.Invoke(ChangeStatus, "ONLINE")

                Else
                    Me.Invoke(ChangeStatus, "OFFLINE")
                End If
            End If
        End If

        Threading.Thread.Sleep(3000)
        ChekStatus()
    End Sub

    Dim ChangeStatus As New delegChangeStatus(AddressOf safeChangeStatus)

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

        If APP.Info.Jenjang = "SMA" Then
            'frm_sma_add.MdiParent = Me
            'frm_sma_add.WindowState = FormWindowState.Maximized
            frm_sma_add.Show(Me)
            frm_sma_add.Focus()
            'frm_sma_edit.Close()
            'frm_list_sma.Close()
        ElseIf APP.Info.Jenjang = "SMP" Then
            'frm_smp_add.MdiParent = Me
            'frm_smp_add.WindowState = FormWindowState.Maximized
            frm_smp_add.Show(Me)
            frm_smp_add.Focus()
            'frm_list_smp.Close()
            'frm_sma_edit.Close()
        ElseIf APP.Info.Jenjang = "SMK" Then
            'frm_smk_add.MdiParent = Me
            'frm_smk_add.WindowState = FormWindowState.Maximized
            frm_smk_add.Show(Me)
            frm_smk_add.Focus()
            'frm_list_smk.Close()
            'frm_smk_edit.Close()

        End If

    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        frm_list_pendaftar.MdiParent = Me
        frm_list_pendaftar.WindowState = FormWindowState.Maximized
        frm_list_pendaftar.Show()
        frm_list_pendaftar.Focus()
    End Sub

    Private Sub mnu_upload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_upload.Click
        frm_upload.Dispose()
        frm_upload.ShowDialog(Me)
    End Sub

    Private Sub frm_main_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            MyThread.Abort()
        Catch ex As Exception

        End Try
        End
    End Sub

    Private Sub frm_main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Text = "Aplikasi PPDB 2012 Versi : " & APP.Version

        If APP.Info.IsServer = True Then
            mnu_upload.Enabled = True
            Call StartThread()
        Else
            mnu_upload.Enabled = False
            StatusOnline.Text = "Ready"
        End If

        '#
        'If APP.Info.KD_Sekolah = "" Then
        Pengkali_UN = 2
        Pengkali_US = 1
        'End If

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

    Private Sub mnu_backup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_backup.Click
        Try
            With SaveFileDialog1

                .Title = "Backup Database"
                .Filter = "File SQL (*.sql)|*.sql"
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    BackupDatabase(.FileName)
                End If

            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub mnu_restore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_restore.Click
        Try
            With OpenFileDialog1

                .Title = "Restore Database"
                .Filter = "File SQL (*.sql)|*.sql"
                .FileName = ""
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    If RestoreDatabase(.FileName) = True Then
                        MsgBox("Proses berhasil dilakukan", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
                    Else
                        MsgBox("Proses gagal dilakukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                    End If
                End If

            End With
        Catch ex As Exception

        End Try
    End Sub
End Class
